// pages/video/video.js
Page({
    /* 页面的初始数据
     */
    data: {
        isRecording: false, //记录拍摄状态
        
    },
      record() {
        if (this.data.isRecording) {
            console.log("结束录制视频")
            this.stopRecord()
        } else {
            console.log("开始录制视频")
            this.startRecord()
        }
    },
    startRecord() {
        var that = this;
        console.log(that);
        that.data.ctx.startRecord({
            timeout: 120,
            success: (res) => {
                that.setData({
                    isRecording: 'ture'
                })
                wx.showModal({
                    title: '提示',
                    content: '开始录制',
                    confirmtext: '好的',
                    cancelColor: 'cancelColor',
                })
            },
            fail: (res) => {
                console.log('失败')
            },
            complete: (res) => {}
        })
    },
    stopRecord() {
        var that = this;
        that.setData({
            mask: true
        })
        //that.stop(); 可以用来计时
        that.data.ctx.stopRecord({
            //compressed: true, //压缩视频
            success: (res) => {
                that.setData({
                    isRecording: 'false',
                    src: res.tempThumbPath, //照片路径
                    videoSrc: res.tempVideoPath // 请注意，这里是所拍视频文件的临时路径，需上传到服务器
                })
                that.submit();
                wx.showModal({
                  title: '提示',
                  content: '结束录制',
                  confirmtext: '好的',
                  cancelColor: 'cancelColor',
              })
            },
            fail: (res) => {
                console.log('停止过程失败')
            },
            complete: function (res) {
                wx.hideToast();
                that.setData({
                    mask: false,
                })
            }
        })
    },
    
    submit() {
        var that = this;
        var video = that.data.videoSrc
        var videoName = Date.now() + ".mp4"
        // 视频存放云端
        wx.cloud.uploadFile({
            cloudPath: videoName,
            filePath: video,
        }).then(res => {
            console.log(res);
            wx.cloud.callFunction({
                name: "setVideoId",
                data: {
                    videoName: videoName,
                    fileId: res.fileID
                }
            }),
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 2000
            })
          }).catch(err => {
            console.log(err);
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        var ctx = wx.createCameraContext();
        that.setData({
            ctx: ctx
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})